$(function() {
	hover_show_dropdown_menu(".dropdown")
	logout("#logout")
	if (check_login_state("http://193.112.72.237/Web-comfun/web/index.php?r=user/checklogin"))
		set_uHeadurl()
})

//鼠标悬停显示下拉菜单
function hover_show_dropdown_menu(selector) {
	if ($.cookie("session") == null) {
		$("#logout").hide()
	} else {
		$("#sign_in").hide()
		$("#sign_up").hide()
	}
	var dropdown = $(selector)
	dropdown.hover(
		function() {
			$(document).off('click.bs.dropdown.data-api');
			$(".dropdown-menu").attr("style", "display:block")
		},
		function() {
			$(".dropdown-menu").attr("style", " ")
			$(document).on('click.bs.dropdown.data-api');
		})
}

//获取用户头像并设置
function set_uHeadurl() {
	var uHeadurl = $.cookie("uHeadurl")
	if (uHeadurl != null) {
		$("#uHead").attr("style", "")
		$("#uHead").empty();
		$("#uHead").append("<img src='" + uHeadurl + "' class='border border-secondary rounded-circle' />")
	}
}

//检查用户登录状态
function check_login_state(url) {
	login_state = false;
	$.ajax({
		type: "POST",
		async: false,
		data: {
			"session": $.cookie("session")
		},
		url: url,
		success: function(data) {
			if (data["code"] == 501) {
				login_state = true
			} else {
				$.removeCookie("session", {
					path: "/"
				})
				$.removeCookie("uName", {
					path: "/"
				})
				$.removeCookie("uHeadurl", {
					path: "/"
				})
			}
		}
	})
	return login_state;
}

//根据 selecter 按钮 执行注销
function logout(selector) {
	$(selector).click(function() {
		$.post("http://193.112.72.237/Web-comfun/web/index.php?r=user/loginout", {
				"session": $.cookie("session")
			},
			function(data) {
				if (data["code"] == 501) {
					$.removeCookie("session", {
						path: "/"
					})
					$.removeCookie("uName", {
						path: "/"
					})
					$.removeCookie("uHeadurl", {
						path: "/"
					})
					alert("注销成功")
					window.location.reload()
				}
			},
		);
	});
}
